#!/usr/bin/python3

color_file = '/home/kristijan/.cache/main_colorscheme.conf'
colorschemes = {
    'dark': 'onenord',
    'light': 'onenord',
}

import pynvim
import argparse
import os
import sys
import time
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument('bg', nargs='?', default=None, choices=['dark', 'light', 'toggle'])

args = parser.parse_args()
bg = args.bg
current_bg = os.getenv('NVIM_COLORSCHEME_BG')

def set_env(background):
    os.system('kitty @ set-colors --all --configured ' + color_file)
    cmd = ['export NVIM_COLORSCHEME_BG=' + background]
    cmd += ['export NVIM_COLORSCHEME=' + colorschemes[background]]
    cmd += ['python ~/neovim-config/kitty/reload_colors.py']
    return cmd

if Path(color_file).exists():
    cfile = open(color_file, 'r')
    first_line = cfile.readline().rstrip()
    current_bg = first_line.split('=')[1]
elif not bg:
    print('You must provide background type argument on first run', file=sys.stderr)
    exit()

if not bg:
    print(';'.join(set_env(current_bg)))
    exit()

if bg == 'toggle':
    bg = 'dark' if current_bg == 'light' else 'light'


os.environ['NVIM_COLORSCHEME_BG'] = bg
os.environ['NVIM_COLORSCHEME'] = colorschemes[bg]
nvim = pynvim.attach('child', argv=["/usr/bin/env", "nvim", "--embed", "--headless"])

def get_hl_color(name):
    hl_output = nvim.call('execute', 'hi ' + name)
    bgcolor = None
    fgcolor = None
    for item in hl_output.split('\n')[1].split():
        if item.startswith('guifg'):
            fgcolor = item.split('=')[1]
        if item.startswith('guibg'):
            bgcolor = item.split('=')[1]
    return fgcolor or '', bgcolor or ''

normal_fg, normal_bg = get_hl_color('Normal')
cursor_fg, cursor_bg = get_hl_color('Cursor')
selection_fg, selection_bg = get_hl_color('Visual')

command = [
    'env NVIM_COLORSCHEME_BG=' + bg,
    'env NVIM_COLORSCHEME=' + colorschemes[bg],
    'foreground ' + normal_fg,
    'background ' + normal_bg,
    'cursor ' + (cursor_bg.startswith('#') and cursor_bg or normal_fg),
    'cursor_text_color background',
    'selection_foreground ' + (selection_fg.startswith('#') and selection_fg or 'none'),
    'selection_background ' + (selection_bg.startswith('#') and selection_bg or normal_fg)
]
for i in range(0, 16):
    command.append('color' + str(i) + ' ' + nvim.eval('g:terminal_color_' + str(i)))

nvim.close()

file = open(color_file, 'w')
file.write('\n'.join(command))
file.close()

cmd = set_env(bg)
is_bg_changed = bg != current_bg
if is_bg_changed:
    xresource_file = open('/home/kristijan/.Xresources_custom', 'w')
    xresource_file.write('*polybar_bg: #FA' + normal_bg[1:] + '\n*polybar_fg:' + normal_fg)
    xresource_file.close()
    is_light = 'true' if bg == 'light' else 'false'
    cmd += ['xrdb ~/.Xresources']
    cmd += ['~/.config/polybar/start.sh']
    cmd += ['source ~/.zshrc']

print(';'.join(cmd))
